package com.wlyuan.open.scanner.utils.mappings;

import com.alibaba.fastjson.JSONObject;
import com.wlyuan.core.domain.valueobj.UserId;
import com.wlyuan.open.scanner.utils.NumberUtils;
import com.wlyuan.open.scanner.utils.enums.AutoBalanceEnum;
import com.wlyuan.open.scanner.utils.enums.CarrierSubjectTypeEnum;
import com.wlyuan.open.scanner.utils.enums.SourceInvalidEnum;
import com.wlyuan.open.scanner.utils.enums.SupplierInvalidEnum;
import com.wlyuan.open.scanner.utils.enums.bill.*;
import com.wlyuan.open.scanner.utils.enums.named.*;
import com.wlyuan.open.scanner.utils.enums.order.*;
import com.wlyuan.open.scanner.utils.enums.quotation.*;
import com.wlyuan.open.scanner.utils.enums.tenant.DriverTenantCheckStatusEnum;
import com.wlyuan.open.scanner.utils.enums.tenant.TenantInvalidEnum;
import com.wlyuan.open.scanner.utils.enums.transport.*;
import com.wlyuan.open.scanner.utils.enums.transport.combine.CombineStatusEnum;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author lijing
 * @description
 */
public class MappingInvalid {

    public BigDecimal toBigDecimalNotNull(BigDecimal source) {
        return NumberUtils.changeZero(source);
    }

    public Integer toIntegerNotNull(BigDecimal source) {
        return NumberUtils.changeZero(source).intValue();
    }

    public UserId createUserId(Long userId) {
        return Optional.ofNullable(userId).filter(v -> v > 0).map(UserId::new).orElse(new UserId(0L));
    }

    /**
     * 状态 禁用-启用
     *
     * @param value
     * @return
     */
    public Integer getStatusValue(SourceInvalidEnum value) {
        return Optional.ofNullable(value).map(SourceInvalidEnum::getNewValue).orElse(null);
    }

    public SourceInvalidEnum createStatus(Integer value) {
        return SourceInvalidEnum.fromValue(value);
    }

    /**
     * 自动对账
     *
     * @param value
     * @return
     */
    public Integer getAutoBalance(AutoBalanceEnum value) {
        return Optional.ofNullable(value).map(AutoBalanceEnum::getNewValue).orElse(null);
    }

    public AutoBalanceEnum createAutoBalance(Integer value) {
        return AutoBalanceEnum.fromValue(value);
    }

    /**
     * 线上承运商状态
     *
     * @param status  状态值
     * @param invalid 有效值
     * @return
     */
    public static SupplierInvalidEnum createSupplierStatus(Long status, Integer invalid) {
        return SupplierInvalidEnum.fromValue(status, invalid);
    }

    /**
     * 租户状态
     *
     * @param invalid 有效值
     * @param status  状态值
     * @return
     */
    public static TenantInvalidEnum createTenantStatus(Integer invalid, Long status) {
        return TenantInvalidEnum.fromValue(invalid, status);
    }

    /**
     * 司机租户其他相关信息状态转换
     *
     * @param value
     * @return
     */
    public Integer getStatusValue(DriverTenantCheckStatusEnum value) {
        return Optional.ofNullable(value).map(DriverTenantCheckStatusEnum::getNewValue).orElse(null);
    }

    /**
     * 司机租户check状态
     *
     * @param value
     * @return
     */
    public DriverTenantCheckStatusEnum createTenantCheckStatus(Integer value) {
        return DriverTenantCheckStatusEnum.fromValue(value);
    }

    /**
     * 订单状态
     *
     * @param value
     * @return
     */
    public Integer getOrderStatusValue(OrderStatusEnum value) {
        return Optional.ofNullable(value).map(OrderStatusEnum::getNewValue).orElse(null);
    }

    public OrderStatusEnum createOrderStatus(Integer value) {
        return OrderStatusEnum.fromValue(value);
    }

    /**
     * 订单签收类型
     *
     * @param value
     * @return
     */
    public Integer getOrderSignTypeValue(OrderSignTypeEnum value) {
        return Optional.ofNullable(value).map(OrderSignTypeEnum::getNewValue).orElse(null);
    }

    public OrderSignTypeEnum createOrderSignType(Integer value) {
        return OrderSignTypeEnum.fromValue(value);
    }

    /**
     * 运单类型
     *
     * @param type
     * @return
     */
    public Integer getTransportTypeValue(TransportTypeEnum type) {
        return Optional.ofNullable(type).map(TransportTypeEnum::getNewValue).orElse(null);
    }

    public TransportTypeEnum createTransportValue(Integer value) {
        return TransportTypeEnum.fromValue(value);
    }

    /**
     * 运单状态
     *
     * @param value
     * @return
     */
    public Integer getTransportStatusValue(TransportStatusEnum value) {
        return Optional.ofNullable(value).map(TransportStatusEnum::getNewValue).orElse(null);
    }

    public TransportStatusEnum createStatusValue(Integer value) {
        return TransportStatusEnum.fromValue(value);
    }

    /**
     * 运单关闭状态
     *
     * @param value
     * @return
     */
    public Integer getTransportCloseStatusValue(TransportCloseStatusEnum value) {
        return Optional.ofNullable(value).map(TransportCloseStatusEnum::getNewValue).orElse(null);
    }

    public TransportCloseStatusEnum createCloseStatusValue(Integer value) {
        return TransportCloseStatusEnum.fromValue(value);
    }

    /**
     * 运单关闭类型
     *
     * @param value
     * @return
     */
    public Integer getTransportSignTypeValue(TransportSignTypeEnum value) {
        return Optional.ofNullable(value).map(TransportSignTypeEnum::getNewValue).orElse(null);
    }

    public TransportSignTypeEnum createTransportSignTypeValue(Integer value) {
        return TransportSignTypeEnum.fromValue(value);
    }

    /**
     * 运单签收类型
     *
     * @param value
     * @return
     */
    public Integer getTransportCloseTypeValue(TransportCloseTypeEnum value) {
        return Optional.ofNullable(value).map(TransportCloseTypeEnum::getNewValue).orElse(null);
    }

    public TransportCloseTypeEnum createCloseTypeValue(Integer value) {
        return TransportCloseTypeEnum.fromValue(value);
    }

    /**
     * 拼车单状态
     *
     * @param value
     * @return
     */
    public Integer getCombineStatusValue(CombineStatusEnum value) {
        return Optional.ofNullable(value).map(CombineStatusEnum::getNewValue).orElse(null);
    }

    public CombineStatusEnum createCombineStatusValue(Integer value) {
        return CombineStatusEnum.fromValue(value);
    }

    /**
     * 合约状态
     *
     * @param value
     * @return
     */
    public Integer getTemplateStatusValue(TemplateStatusEnum value) {
        return Optional.ofNullable(value).map(TemplateStatusEnum::getNewValue).orElse(null);
    }

    public TemplateStatusEnum createTemplateStatusValue(Integer value) {
        return TemplateStatusEnum.fromValue(value);
    }

    /**
     * 报价状态
     *
     * @param value
     * @return
     */
    public Integer getQuotationStatusValue(QuotationStatusEnum value) {
        return Optional.ofNullable(value).map(QuotationStatusEnum::getNewValue).orElse(null);
    }

    public QuotationStatusEnum createQuotationStatusValue(Integer value) {
        return QuotationStatusEnum.fromValue(value);
    }

    /**
     * 计费模式
     *
     * @param value
     * @return
     */
    public Integer getChargeModeValue(ChargeModeEnum value) {
        return Optional.ofNullable(value).map(ChargeModeEnum::getNewValue).orElse(null);
    }

    public ChargeModeEnum createChargeModeValue(Integer value) {
        return ChargeModeEnum.fromValue(value);
    }

    /**
     * 货物分类
     *
     * @param value
     * @return
     */
    public Integer getGoodsCategoryValue(GoodsCategoryEnum value) {
        return Optional.ofNullable(value).map(GoodsCategoryEnum::getNewValue).orElse(null);
    }

    public GoodsCategoryEnum createGoodsCategoryValue(Integer value) {
        return GoodsCategoryEnum.fromValue(value);
    }

    /**
     * 包装方式
     *
     * @param value
     * @return
     */
    public Integer getPackageModeValue(PackageModeEnum value) {
        return Optional.ofNullable(value).map(PackageModeEnum::getNewValue).orElse(null);
    }

    public PackageModeEnum createPackageValue(Integer value) {
        return PackageModeEnum.fromValue(value);
    }

    /**
     * 计价单位
     *
     * @param value
     * @return
     */
    public Integer getPriceUnitValue(PriceUnitEnum value) {
        return Optional.ofNullable(value).map(PriceUnitEnum::getNewValue).orElse(null);
    }

    public PriceUnitEnum createPriceUnitValue(Integer value) {
        return PriceUnitEnum.fromValue(value);
    }

    /**
     * 追踪方式
     *
     * @param value
     * @return
     */
    public Integer getTrackTypeValue(TrackTypeEnum value) {
        return Optional.ofNullable(value).map(TrackTypeEnum::getNewValue).orElse(null);
    }

    public TrackTypeEnum createTrackTypeValue(Integer value) {
        return TrackTypeEnum.fromValue(value);
    }

    /**
     * 运输模式
     *
     * @param value
     * @return
     */
    public Integer getTransportModeValue(TransportModeEnum value) {
        return Optional.ofNullable(value).map(TransportModeEnum::getNewValue).orElse(null);
    }

    public TransportModeEnum createTransportModeValue(Integer value) {
        return TransportModeEnum.fromValue(value);
    }

    /**
     * 订单创建类型
     *
     * @param value
     * @return
     */
    public Integer getOrderCreateTypeValue(OrderCreateTypeEnum value) {
        return Optional.ofNullable(value).map(OrderCreateTypeEnum::getNewValue).orElse(null);
    }

    public OrderCreateTypeEnum createOrderCreateTypeValue(Integer value) {
        return OrderCreateTypeEnum.fromValue(value);
    }

    /**
     * 对账单状态
     *
     * @param value
     * @return
     */
    public Integer getBillStatusValue(BillStatusEnum value) {
        return Optional.ofNullable(value).map(BillStatusEnum::getNewValue).orElse(null);
    }

    public BillStatusEnum createBillStatusValue(Integer value) {
        return BillStatusEnum.fromValue(value);
    }

    /**
     * 对账单类型
     *
     * @param value
     * @return
     */
    public Integer getBillTypeValue(BillTypeEnum value) {
        return Optional.ofNullable(value).map(BillTypeEnum::getNewValue).orElse(null);
    }

    public BillTypeEnum createBillTypeValue(Integer value) {
        return BillTypeEnum.fromValue(value);
    }

    /**
     * 付款状态
     *
     * @param value
     * @return
     */
    public Integer getPayFinishValue(PayStatusEnum value) {
        return Optional.ofNullable(value).map(PayStatusEnum::getNewValue).orElse(null);
    }

    public PayStatusEnum createPayFinishValue(Integer value) {
        return PayStatusEnum.fromValue(value);
    }

    public Integer getPayModeValue(PayModeEnum value) {
        return Optional.ofNullable(value).map(PayModeEnum::getNewValue).orElse(null);
    }

    public PayModeEnum createPayModeValue(Integer value) {
        return PayModeEnum.fromValue(value);
    }

    public Integer getPayTypeValue(PayTypeEnum value) {
        return Optional.ofNullable(value).map(PayTypeEnum::getNewValue).orElse(null);
    }

    public PayTypeEnum createPayTypeValue(Integer value) {
        return PayTypeEnum.fromValue(value);
    }

    public Integer getReceiveFinishValue(ReceiveFinishEnum value) {
        return Optional.ofNullable(value).map(ReceiveFinishEnum::getNewValue).orElse(null);
    }

    public ReceiveFinishEnum createReceiveFinishValue(Integer value) {
        return ReceiveFinishEnum.fromValue(value);
    }

    public Integer getTransportFinishStatusValue(TransportBillStatusEnum value) {
        return Optional.ofNullable(value).map(TransportBillStatusEnum::getNewValue).orElse(null);
    }

    public TransportBillStatusEnum createTransportFinishStatusValue(Integer value) {
        return TransportBillStatusEnum.fromValue(value);
    }

    public Integer getTemplateTypeValue(TemplateModeEnum value) {
        return Optional.ofNullable(value).map(TemplateModeEnum::getNewValue).orElse(null);
    }

    public TemplateModeEnum createTemplateTypeValue(Integer value) {
        return TemplateModeEnum.fromValue(value);
    }

    public Integer getTemplateFromTypeValue(TemplateFromTypeEnum value) {
        return Optional.ofNullable(value).map(TemplateFromTypeEnum::getNewValue).orElse(null);
    }

    public TemplateFromTypeEnum createTemplateFromTypeValue(Integer value) {
        return TemplateFromTypeEnum.fromValue(value);
    }

    public Integer getAttachTypeValue(AttachTypeEnum value) {
        return Optional.ofNullable(value).map(AttachTypeEnum::getNewValue).orElse(null);
    }

    public AttachTypeEnum createAttachTypeValue(Integer value) {
        return AttachTypeEnum.fromValue(value);
    }

    public Integer getAttachTypeValue(TransportAttachTypeEnum value) {
        return Optional.ofNullable(value).map(TransportAttachTypeEnum::getNewValue).orElse(null);
    }

    public TransportAttachTypeEnum createTransportAttachTypeValue(Integer value) {
        return TransportAttachTypeEnum.fromValue(value);
    }

    public Integer getMultiTransportTypeValue(MultiTransportTypeEnum value) {
        return Optional.ofNullable(value).map(MultiTransportTypeEnum::getNewValue).orElse(null);
    }

    public MultiTransportTypeEnum createMultiTransportTypeValue(Integer value) {
        return MultiTransportTypeEnum.fromValue(value);
    }

    public Integer getQuotationSubjectTypeValue(QuotationSubjectTypeEnum value) {
        return Optional.ofNullable(value).map(QuotationSubjectTypeEnum::getNewValue).orElse(null);
    }

    public QuotationSubjectTypeEnum createQuotationSubjectTypeValue(Long value) {
        return QuotationSubjectTypeEnum.fromValue(value);
    }

    public Integer getQuotationSurchargeTypeValue(QuotationSurchargeTypeEnum value) {
        return Optional.ofNullable(value).map(QuotationSurchargeTypeEnum::getNewValue).orElse(null);
    }

    public QuotationSurchargeTypeEnum createQuotationSurchargeTypeValue(Integer value) {
        return Optional.ofNullable(QuotationSurchargeTypeEnum.fromValue(value)).orElse(QuotationSurchargeTypeEnum.OTHER);
    }

    public Integer getWayBillType(WaybillTypeEnum value) {
        return Optional.ofNullable(value).map(WaybillTypeEnum::getNewValue).orElse(null);
    }

    public WaybillTypeEnum createWayBillType(Long value) {
        return WaybillTypeEnum.fromValue(value);
    }

    public List<TransportScoreLabelEnum> createScoreLabel(String value) {
        if (StringUtils.isBlank(value)) {
            return Collections.emptyList();
        }
        var list = Arrays.asList(value.split("；"));
        return list.stream().map(x -> TransportScoreLabelEnum.fromValue(x)).filter(Objects::nonNull).collect(Collectors.toList());
    }

    public String getScoreLabel(List<TransportScoreLabelEnum> list) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        return String.join(",", list.stream().map(TransportScoreLabelEnum::getValue).collect(Collectors.toList()));
    }

    public Integer getAttachSourceValue(AttachSourceEnum value) {
        return Optional.ofNullable(value).map(AttachSourceEnum::getNewValue).orElse(0);
    }

    public AttachSourceEnum createAttachSourceValue(Integer value) {
        return AttachSourceEnum.fromValue(value);
    }

    public CarrierSubjectTypeEnum createCarrierSubjectTypeValue(Integer value) {
        return CarrierSubjectTypeEnum.fromValue(value);
    }

    public Integer getTopOrderType(TopOrderTypeEnum value) {
        return Optional.ofNullable(value).map(TopOrderTypeEnum::getNewValue).orElse(0);
    }

    public TopOrderTypeEnum getTopOrderType(Integer value) {
        return TopOrderTypeEnum.fromValue(value);
    }

    public Integer getCarrierSubjectSubjectTypeValue(CarrierSubjectTypeEnum value) {
        return Optional.ofNullable(value).map(CarrierSubjectTypeEnum::getValue).orElse(null);
    }

    public JSONObject strToJsonObj(String value) {
        if (StringUtils.isBlank(value)) {
            return new JSONObject();
        }
        try {
            return JSONObject.parseObject(value);
        } catch (Exception e) {
            return new JSONObject();
        }
    }
}
